In [1]:
%matplotlib inline

In [2]:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import mpld3
from mpld3 import plugins, utils
import math

In [3]:
class LinkedView(plugins.PluginBase):
    """A simple plugin showing how multiple axes can be linked"""

    JAVASCRIPT = """
    mpld3.register_plugin("linkedview", LinkedViewPlugin);
    LinkedViewPlugin.prototype = Object.create(mpld3.Plugin.prototype);
    LinkedViewPlugin.prototype.constructor = LinkedViewPlugin;
    LinkedViewPlugin.prototype.requiredProps = ["idpts", "idline", "data"];
    LinkedViewPlugin.prototype.defaultProps = {}
    function LinkedViewPlugin(fig, props){
        mpld3.Plugin.call(this, fig, props);
    };

    LinkedViewPlugin.prototype.draw = function(){
      var pts = mpld3.get_element(this.props.idpts);
      var line = mpld3.get_element(this.props.idline);
      var data = this.props.data;

      function mouseover(d, i){
        line.data = data[i];
        line.elements().transition()
            .attr("d", line.datafunc(line.data))
            .style("stroke", this.style.fill);
      }
      pts.elements().on("mouseover", mouseover);
    };
    """

    def __init__(self, points, line, linedata):
        if isinstance(points, matplotlib.lines.Line2D):
            suffix = "pts"
        else:
            suffix = None

        self.dict_ = {"type": "linkedview",
                      "idpts": utils.get_id(points, suffix),
                      "idline": utils.get_id(line),
                      "data": linedata}

In [4]:
nmax=1e6

In [126]:
# scatter periods and amplitudes
np.random.seed(0)
P = np.linspace(-math.log10(nmax), 0, num=20)
#P = 0.2 + np.random.random(size=20)

#M = nmax*np.random.random(size=20)
M = np.ceil(np.linspace(1,nmax,num=20))
x = np.ceil(np.linspace(1,nmax,num=100))
data = np.array([[x, ((x<Mi)*(x-Mi) + Mi )*x ]
                 for (Mi, Pi) in zip(M, P)])

In [127]:
Mi=1e5
((x<Mi)*x + (x>Mi)*([Mi]*len(x)) )
((x<Mi)*(x-Mi) + Mi )


Out[127]:
array([  1.00000000e+00,   1.01020000e+04,   2.02030000e+04,
         3.03040000e+04,   4.04050000e+04,   5.05060000e+04,
         6.06070000e+04,   7.07080000e+04,   8.08090000e+04,
         9.09100000e+04,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05,   1.00000000e+05,   1.00000000e+05,
         1.00000000e+05])

In [128]:
cdict = {'red':   [(0.0,  0.0, 0.0),
                   (1.0,  1.0, 1.0)],

         'green': [(0.0,  0.0, 0.0),
                   (1.0,  0.0, 0.0)],

         'blue':  [(0.0,  0.0, 0.0),
                   (1.0,  0.0, 0.0)]}
cmap = matplotlib.colors.LinearSegmentedColormap('Red', cdict)

In [129]:
x


Out[129]:
array([  1.00000000e+00,   1.01020000e+04,   2.02030000e+04,
         3.03040000e+04,   4.04050000e+04,   5.05060000e+04,
         6.06070000e+04,   7.07080000e+04,   8.08090000e+04,
         9.09100000e+04,   1.01011000e+05,   1.11112000e+05,
         1.21213000e+05,   1.31314000e+05,   1.41415000e+05,
         1.51516000e+05,   1.61617000e+05,   1.71718000e+05,
         1.81819000e+05,   1.91920000e+05,   2.02021000e+05,
         2.12122000e+05,   2.22223000e+05,   2.32324000e+05,
         2.42425000e+05,   2.52526000e+05,   2.62627000e+05,
         2.72728000e+05,   2.82829000e+05,   2.92930000e+05,
         3.03031000e+05,   3.13132000e+05,   3.23233000e+05,
         3.33334000e+05,   3.43435000e+05,   3.53536000e+05,
         3.63637000e+05,   3.73738000e+05,   3.83839000e+05,
         3.93940000e+05,   4.04041000e+05,   4.14142000e+05,
         4.24243000e+05,   4.34344000e+05,   4.44445000e+05,
         4.54546000e+05,   4.64647000e+05,   4.74748000e+05,
         4.84849000e+05,   4.94950000e+05,   5.05051000e+05,
         5.15152000e+05,   5.25253000e+05,   5.35354000e+05,
         5.45455000e+05,   5.55556000e+05,   5.65657000e+05,
         5.75758000e+05,   5.85859000e+05,   5.95960000e+05,
         6.06061000e+05,   6.16162000e+05,   6.26263000e+05,
         6.36364000e+05,   6.46465000e+05,   6.56566000e+05,
         6.66667000e+05,   6.76768000e+05,   6.86869000e+05,
         6.96970000e+05,   7.07071000e+05,   7.17172000e+05,
         7.27273000e+05,   7.37374000e+05,   7.47475000e+05,
         7.57576000e+05,   7.67677000e+05,   7.77778000e+05,
         7.87879000e+05,   7.97980000e+05,   8.08081000e+05,
         8.18182000e+05,   8.28283000e+05,   8.38384000e+05,
         8.48485000e+05,   8.58586000e+05,   8.68687000e+05,
         8.78788000e+05,   8.88889000e+05,   8.98990000e+05,
         9.09091000e+05,   9.19192000e+05,   9.29293000e+05,
         9.39394000e+05,   9.49495000e+05,   9.59596000e+05,
         9.69697000e+05,   9.79798000e+05,   9.89899000e+05,
         1.00000000e+06])

In [141]:
fig, ax = plt.subplots(2)

points = ax[1].scatter(M, P, c=-(P+min(P))/min(P), cmap=cmap, 
                       s=400*(M/nmax), alpha=0.5)
ax[1].set_xlabel('# machines')
ax[1].set_xlim(1,nmax)
ax[1].set_ylabel('log(Fraction of reds)')
ax[1].set_ylim(-math.log10(nmax), 0)


# create the line object
lines = ax[0].plot(x, 0 * x, '-w', lw=3, alpha=0.5)

ax[0].set_xlabel('# nodes')
ax[0].set_xlim(0, nmax)
ax[0].set_ylabel('# messages')
ax[0].set_ylim(1, max(max(linedata))[1])
ax[0].set_title("Hover over points to see lines")

# transpose line data and add plugin
linedata = data.transpose(0, 2, 1).tolist()
plugins.connect(fig, LinkedView(points, lines[0], linedata))
fig.tight_layout()

mpld3.display()


Out[141]:

In [136]:
max(max(linedata))[1]


Out[136]:
1000000000000.0

In [137]:
fig2, ax2 = plt.subplots(1)



In [109]:
ax2.plot(x,linedata)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-109-c7a7797f5ccf> in <module>()
----> 1 ax2.plot(x,linedata)

/Users/migish/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in plot(self, *args, **kwargs)
   4135         lines = []
   4136 
-> 4137         for line in self._get_lines(*args, **kwargs):
   4138             self.add_line(line)
   4139             lines.append(line)

/Users/migish/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in _grab_next_args(self, *args, **kwargs)
    315                 return
    316             if len(remaining) <= 3:
--> 317                 for seg in self._plot_args(remaining, kwargs):
    318                     yield seg
    319                 return

/Users/migish/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in _plot_args(self, tup, kwargs)
    293             x = np.arange(y.shape[0], dtype=float)
    294 
--> 295         x, y = self._xy_from_xy(x, y)
    296 
    297         if self.command == 'plot':

/Users/migish/anaconda/lib/python2.7/site-packages/matplotlib/axes.pyc in _xy_from_xy(self, x, y)
    235         y = np.atleast_1d(y)
    236         if x.shape[0] != y.shape[0]:
--> 237             raise ValueError("x and y must have same first dimension")
    238         if x.ndim > 2 or y.ndim > 2:
    239             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension

In [121]:
linedata[0]


Out[121]:
[[1.0, 1.0],
 [10102.0, 1.0],
 [20203.0, 1.0],
 [30304.0, 1.0],
 [40405.0, 1.0],
 [50506.0, 1.0],
 [60607.0, 1.0],
 [70708.0, 1.0],
 [80809.0, 1.0],
 [90910.0, 1.0],
 [101011.0, 1.0],
 [111112.0, 1.0],
 [121213.0, 1.0],
 [131314.0, 1.0],
 [141415.0, 1.0],
 [151516.0, 1.0],
 [161617.0, 1.0],
 [171718.0, 1.0],
 [181819.0, 1.0],
 [191920.0, 1.0],
 [202021.0, 1.0],
 [212122.0, 1.0],
 [222223.0, 1.0],
 [232324.0, 1.0],
 [242425.0, 1.0],
 [252526.0, 1.0],
 [262627.0, 1.0],
 [272728.0, 1.0],
 [282829.0, 1.0],
 [292930.0, 1.0],
 [303031.0, 1.0],
 [313132.0, 1.0],
 [323233.0, 1.0],
 [333334.0, 1.0],
 [343435.0, 1.0],
 [353536.0, 1.0],
 [363637.0, 1.0],
 [373738.0, 1.0],
 [383839.0, 1.0],
 [393940.0, 1.0],
 [404041.0, 1.0],
 [414142.0, 1.0],
 [424243.0, 1.0],
 [434344.0, 1.0],
 [444445.0, 1.0],
 [454546.0, 1.0],
 [464647.0, 1.0],
 [474748.0, 1.0],
 [484849.0, 1.0],
 [494950.0, 1.0],
 [505051.0, 1.0],
 [515152.0, 1.0],
 [525253.0, 1.0],
 [535354.0, 1.0],
 [545455.0, 1.0],
 [555556.0, 1.0],
 [565657.0, 1.0],
 [575758.0, 1.0],
 [585859.0, 1.0],
 [595960.0, 1.0],
 [606061.0, 1.0],
 [616162.0, 1.0],
 [626263.0, 1.0],
 [636364.0, 1.0],
 [646465.0, 1.0],
 [656566.0, 1.0],
 [666667.0, 1.0],
 [676768.0, 1.0],
 [686869.0, 1.0],
 [696970.0, 1.0],
 [707071.0, 1.0],
 [717172.0, 1.0],
 [727273.0, 1.0],
 [737374.0, 1.0],
 [747475.0, 1.0],
 [757576.0, 1.0],
 [767677.0, 1.0],
 [777778.0, 1.0],
 [787879.0, 1.0],
 [797980.0, 1.0],
 [808081.0, 1.0],
 [818182.0, 1.0],
 [828283.0, 1.0],
 [838384.0, 1.0],
 [848485.0, 1.0],
 [858586.0, 1.0],
 [868687.0, 1.0],
 [878788.0, 1.0],
 [888889.0, 1.0],
 [898990.0, 1.0],
 [909091.0, 1.0],
 [919192.0, 1.0],
 [929293.0, 1.0],
 [939394.0, 1.0],
 [949495.0, 1.0],
 [959596.0, 1.0],
 [969697.0, 1.0],
 [979798.0, 1.0],
 [989899.0, 1.0],
 [1000000.0, 1.0]]

In [ ]: